package gunfire.cards;

import basemod.abstracts.CustomCard;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.vfx.ThoughtBubble;
import gunfire.GunfireMod;
import gunfire.actions.ChooseOneAddToDeckAction;
import gunfire.characters.Taizi;

import java.util.ArrayList;
import java.util.Collections;

import static gunfire.GunfireMod.makeCardPath;

public class Genesis extends CustomCard {

    public static final String ID = GunfireMod.makeID(Genesis.class.getSimpleName());
    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG = makeCardPath("Genesis.png");
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardType TYPE = CardType.SKILL;
    private static final CardTarget TARGET = CardTarget.NONE;
    public static final CardColor COLOR = Taizi.Enums.COLOR_GRAY;
    private static final int COST = 2;

    public Genesis() {
        super(ID, NAME, IMG, COST, DESCRIPTION, TYPE, COLOR, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 3;
        this.exhaust = true;
        this.isEthereal = true;
    }

    @Override
    public void upgrade() {
        if (!this.upgraded) {
            this.upgradeName();
            this.upgradeMagicNumber(2);
        }
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        ArrayList<AbstractCard> deck = new ArrayList<>();
        for(AbstractCard c : AbstractDungeon.player.masterDeck.group){
            if(!c.cardID.equals("gunfire:Genesis")){
                AbstractCard cc = c.makeCopy();
                if(c.upgraded){
                    cc.upgrade();
                }
                deck.add(cc);
            }
        }

        CardGroup choices = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);
        if(deck.size() > 0){
            Collections.shuffle(deck, new java.util.Random(AbstractDungeon.shuffleRng.randomLong()));
            for (int i = 0; i < Math.min(this.magicNumber, deck.size()); i++) {
                choices.addToBottom(deck.get(i));
            }
            this.addToBot(new ChooseOneAddToDeckAction(choices));
        } else {
            AbstractDungeon.effectList.add(new ThoughtBubble(AbstractDungeon.player.dialogX, AbstractDungeon.player.dialogY, 3.0F,
                    "There are no other cards in my deck！",
                    true));
        }
    }

    @Override
    public AbstractCard makeCopy() {
        return new Genesis();
    }
}
